package com.dianneng.show.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.dianneng.show.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class ItemLayout extends RelativeLayout {

    @BindView(R.id.tv_left)
    public MarqueeTextView tvLeft;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.tv_right)
    public  MarqueeTextView tvRight;
    @BindView(R.id.iv_line)
    ImageView ivLine;

    private Unbinder unbinder;
    public ItemLayout(Context context) {
        super(context);
    }

    public ItemLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_layout, this);
        unbinder = ButterKnife.bind(this, view);

        TypedArray thisArray = context.obtainStyledAttributes(attrs, R.styleable.ItemLayout);
        String leftText = thisArray.getString(R.styleable.ItemLayout_leftText);
        String rightText = thisArray.getString(R.styleable.ItemLayout_rightText);
        boolean line = thisArray.getBoolean(R.styleable.ItemLayout_line, true);
        boolean rightArrow = thisArray.getBoolean(R.styleable.ItemLayout_rightArrow, true);
        int gravity=thisArray.getInteger(R.styleable.ItemLayout_gravity,1);

        if (TextUtils.isEmpty(leftText)){
            tvLeft.setVisibility(GONE);
        }else {
            tvLeft.setVisibility(VISIBLE);
            tvLeft.setText(leftText);
        }
        if (TextUtils.isEmpty(rightText)){
            tvRight.setVisibility(GONE);
        }else {
            tvRight.setVisibility(VISIBLE);
            tvRight.setText(rightText);
        }
        if (line){
            ivLine.setVisibility(VISIBLE);
        }else {
            ivLine.setVisibility(GONE);
        }
        if (rightArrow){
            ivRight.setVisibility(VISIBLE);
        }else {
            ivRight.setVisibility(INVISIBLE);
        }
        if (gravity==1)
            tvRight.setGravity(Gravity.RIGHT);
        else
            tvRight.setGravity(Gravity.LEFT);
    }

    public void setLeftText(String text){
        tvLeft.setVisibility(VISIBLE);
        tvLeft.setText(text);
    }

    public void setRightText(String text){
        tvRight.setVisibility(VISIBLE);
        tvRight.setText(text);
    }

    public void setLine(boolean line){
        if (line){
            ivLine.setVisibility(VISIBLE);
        }else {
            ivLine.setVisibility(GONE);
        }
    }

    public void setRightArrow(boolean rightArrow){
        if (rightArrow){
            ivRight.setVisibility(VISIBLE);
        }else {
            ivRight.setVisibility(INVISIBLE);
        }
    }

    @Override
    protected void detachViewFromParent(View child) {
        super.detachViewFromParent(child);
        unbinder.unbind();
    }
}
